package net.lukecloud.xile.activity;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import net.lukecloud.xile.R;

public class LauncherActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_luncher);
    }

    @Override
    protected void onStart() {
        super.onStart();
        new LuncherTask().execute();
    }


    class LuncherTask extends AsyncTask<Void,Void,Void>{
        String id_token;
        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(1000l);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            SharedPreferences account_info = getSharedPreferences("ACCOUNT_INFO", 0);
            id_token = account_info.getString("id_token", "");
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            if (id_token==null||id_token.length()<190){
                startActivityForResult(new Intent(LauncherActivity.this,LoginActivity.class),requestCode_LOGIN);
            }else{
                startActivity(new Intent(LauncherActivity.this,MainActivity.class));
                finish();
            }
        }
    }

    final int requestCode_LOGIN=123;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==requestCode_LOGIN&& resultCode==RESULT_OK){
            startActivity(new Intent(LauncherActivity.this,MainActivity.class));
            finish();
        }
    }

}
